package tech.aistar.corejava.day10.dat10work;

import java.util.Scanner;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:找俩个字符串中的最大长度的公串
 * @date 2019/7/27 0027
 */
public class StringCommonDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请您输入第一个字符串:>");
        String first = sc.nextLine();

        System.out.println("请您输入第二个字符串:>");
        String two = sc.nextLine();

        printCommon(first,two);
    }

    private static void printCommon(String first, String two) {
        //定义一个变量
        int len = 0;//上一个子串的长度

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < two.length(); i++) {
            for (int j = i+1; j <= two.length() ; j++) {
                //截取 - 将第二个字符串的所有的子串全部找出来.
                String substr = two.substring(i,j);

                //获取子串的长度.
                int subLen = substr.length();

                //判断这些子串是否在first中存在.
                if(first.contains(substr) && subLen>len){
                    //System.out.println(substr);
                    //考虑是否放入到builder

                    //清空builder
                    builder.delete(0,builder.capacity());

                    builder.append(substr+" ");

                    //把当前的子串的长度给len
                    len = subLen;
                }else if(first.contains(substr) && subLen == len){
                    builder.append(substr);
                }
            }
        }
        System.out.println(builder);
    }
}
